<?php
namespace app\agent\controller;
use app\common\model\CardConsume;
use app\common\model\CardGeneral;
use app\common\model\CardMember;
use app\common\model\CardUserUpdate;
use app\common\controller\AdminBase;
use app\common\model\Merchant;
use think\Controller;
use think\Db;
use think\Session;
class Report extends AdminBase
{
    public function index()
    {
        return $this->fetch();
    }

    //会员卡核销详情
    public function reportCardMember(){
        if (date('H') < 2) {
            $this->error('系统正在出账期，请稍后再试');
        }
        $CardUserUpdate = new CardUserUpdate();
        $CardMember = new CardMember();
        $request = request();
        $p_data = $request->get();
        unset($p_data['page']);
        if ($p_data) {
            if ($p_data['mid'] != ''){
                $map['mid'] = array('like', '%' . trim($p_data['mid']) . '%');
            }
            if ($p_data['title'] != ''){
                $map['title'] = array('like', '%' . trim($p_data['title']) . '%');
            }
        } else {
            $p_data['mid'] = '';
            $p_data['title'] = '';
        }
        $map['mid'] = ['in',$this->get_mer_mid()];
        $list = $CardMember->where($map)->paginate(10);
        //今日统计
        $today = strtotime(date("Y-m-d"),time());
        $enday = $today+86399;
        $day['create_time'] = ['between',[$today,$enday]];
        $day['mid'] = ['in',$this->get_mer_mid()];
        //本周统计
        $toweek = strtotime(date('Y-m-d', strtotime("this week Monday", time())));
        $enweek =  strtotime(date('Y-m-d', strtotime("this week Sunday", time()))) + 24 * 3600 - 1;
        $week['create_time'] = ['between',[$toweek,$enweek]];
        $week['mid'] = ['in',$this->get_mer_mid()];
        //本月统计
        $tomonth =mktime(0, 0, 0, date('m'), 1, date('Y'));
        $enmonth =mktime(23, 59, 59, date('m'), date('t'), date('Y'));
        $month['create_time'] = ['between',[$tomonth,$enmonth]];
        $month['mid'] = ['in',$this->get_mer_mid()];
        //数据输出
        //日
        $daysum['numsum'] = $CardUserUpdate->where($day)->count('id');
        $daysum['bonsum'] = $CardUserUpdate->where($day)->sum('del_bonus');
        $daysum['balsum'] = $CardUserUpdate->where($day)->sum('del_balance');
        //周
        $weeksum['numsum'] = $CardUserUpdate->where($week)->count('id');
        $weeksum['bonsum'] = $CardUserUpdate->where($week)->sum('del_bonus');
        $weeksum['balsum'] = $CardUserUpdate->where($week)->sum('del_balance');
        //月
        $monthsum['numsum'] = $CardUserUpdate->where($month)->count('id');
        $monthsum['bonsum'] = $CardUserUpdate->where($month)->sum('del_bonus');
        $monthsum['balsum'] = $CardUserUpdate->where($month)->sum('del_balance');

        $Merchant = new Merchant();
        $mab['id'] = $this->get_mer_mid();
        $mach = $Merchant->where($mab)->select();
        $this->assign('mach',$mach);
        $this->assign('daysum',$daysum);
        $this->assign('weeksum',$weeksum);
        $this->assign('monthsum',$monthsum);
        $this->assign('p_data',$p_data);
        $this->assign('list',$list);
        return $this->fetch();
    }

    //会员卡核销记录
    public function reportcardmemberlist($card_id=''){
        $request = request();
        $p_data = $request->get();
        unset($p_data['page']);
        if ($p_data) {
            if ($p_data['code'] != ''){
                $map['code'] = array('like', '%' . trim($p_data['code']) . '%');
            }
            if ($p_data['mid'] != ''){
                $map['mid'] = array('like', '%' . trim($p_data['mid']) . '%');
            }
            if ($p_data['con_nickname'] != ''){
                $map['con_nickname'] = array('like', '%' . trim($p_data['con_nickname']) . '%');
            }
        } else {
            $p_data['code'] = '';
            $p_data['mid'] = '';
            $p_data['con_nickname'] = '';
        }
        if (empty($card_id)){
        }else{
            $map['card_id'] = ['eq',$card_id];
        }
        $toweek = strtotime(date('Y-m-d', strtotime("this week Monday", time())));
        $enweek =  strtotime(date('Y-m-d', strtotime("this week Sunday", time()))) + 24 * 3600 - 1;
        $map['create_time'] = array('between', '' . $toweek . ',' . $enweek . '');
        $map['type'] = 2;
        $CardUserUpdate = new CardUserUpdate();
        $list = $CardUserUpdate->where($map)->paginate('20');
        //门店
        $Merchant = new Merchant();
        $mab['id'] = $this->get_mer_mid();
        $mach = $Merchant->where($mab)->select();
        $this->assign('mach',$mach);
        $this->assign('p_data',$p_data);
        $this->assign('list',$list);
        return $this->fetch();
    }


    //卡券核销记录
    public function reportcardgenerallist($card_id=''){
        $request = request();
        $p_data = $request->get();
        unset($p_data['page']);
        if ($p_data) {
            if ($p_data['code'] != ''){
                $map['code'] = array('like', '%' . trim($p_data['code']) . '%');
            }
            if ($p_data['mid'] != ''){
                $map['mid'] = array('like', '%' . trim($p_data['mid']) . '%');
            }
            if ($p_data['con_nickname'] != ''){
                $map['con_nickname'] = array('like', '%' . trim($p_data['con_nickname']) . '%');
            }
        } else {
            $p_data['code'] = '';
            $p_data['mid'] = '';
            $p_data['con_nickname'] = '';
        }
        if (empty($card_id)){
        }else{
            $map['card_id'] = ['eq',$card_id];
        }
        $toweek = strtotime(date('Y-m-d', strtotime("this week Monday", time())));
        $enweek =  strtotime(date('Y-m-d', strtotime("this week Sunday", time()))) + 24 * 3600 - 1;
        $map['create_time'] = array('between', '' . $toweek . ',' . $enweek . '');
        $CardConsume = new CardConsume();
        $list = $CardConsume->where($map)->paginate(20);
        //门店
        $Merchant = new Merchant();
        $mab['id'] = $this->get_mer_mid();
        $mach = $Merchant->where($mab)->select();
        $this->assign('mach',$mach);
        $this->assign('list',$list);
        $this->assign('p_data',$p_data);
        return $this->fetch();
    }


    //卡券核销详情
    public function reportcardgeneral(){
        if (date('H') < 2) {
            $this->error('系统正在出账期，请稍后再试');
        }
        $CardConsume = new CardConsume();
        $CardGeneral = new CardGeneral();
        $top_mid = Session::get('login_top_mid');
        $request = request();
        $p_data = $request->get();
        unset($p_data['page']);
        if ($p_data) {
            if ($p_data['mid'] != ''){
                $map['mid'] = array('like', '%' . trim($p_data['mid']) . '%');
            }
            if ($p_data['title'] != ''){
                $map['title'] = array('like', '%' . trim($p_data['title']) . '%');
            }
        } else {
            $p_data['mid'] = '';
            $p_data['title'] = '';
        }
        $map['top_mid'] = ['in',$top_mid];
        $list = $CardGeneral->where($map)->paginate(20);
        //今日统计
        $today = strtotime(date("Y-m-d"),time());
        $enday = $today+86399;
        $day['create_time'] = ['between',[$today,$enday]];
        $map['top_mid'] = ['in',$this->get_mer_mid()];
        //本周统计
        $toweek = strtotime(date('Y-m-d', strtotime("this week Monday", time())));
        $enweek =  strtotime(date('Y-m-d', strtotime("this week Sunday", time()))) + 24 * 3600 - 1;
        $week['create_time'] = ['between',[$toweek,$enweek]];
        $week['mid'] = ['in',$this->get_mer_mid()];
        //本月统计
        $tomonth =mktime(0, 0, 0, date('m'), 1, date('Y'));
        $enmonth =mktime(23, 59, 59, date('m'), date('t'), date('Y'));
        $month['create_time'] = ['between',[$tomonth,$enmonth]];
        $map['top_mid'] = ['in',$this->get_mer_mid()];
        //数据输出
        //日
        $sum['day'] = $CardConsume->where($day)->count('id');
        //周
        $sum['week'] = $CardConsume->where($week)->count('id');
        //月
        $sum['month'] = $CardConsume->where($month)->count('id');
        //卡券数量
        $sum['card'] = $CardGeneral->where($map)->count('id');
        //门店
        $Merchant = new Merchant();
        $mab['id'] = $this->get_mer_mid();
        $mach = $Merchant->where($mab)->select();
        $this->assign('mach',$mach);
        $this->assign('sum',$sum);
        $this->assign('p_data',$p_data);
  //      $this->assign('count',$count);
        $this->assign('list',$list);
        return $this->fetch();
    }

}
